package com.ruoyi.erp.customs.service.impl;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.erp.customs.domain.Customs;
import com.ruoyi.erp.customs.mapper.CustomsMapper;
import com.ruoyi.erp.customs.service.CustomsService;
import com.ruoyi.erp.transport.domian.TransportUnitType;
import com.ruoyi.erp.transport.service.impl.TransportUnitTypeServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class CustomsServiceImpl implements CustomsService {
    private static final Logger log = LoggerFactory.getLogger(TransportUnitTypeServiceImpl.class);
    @Autowired
    CustomsMapper customsMapper;

    @Override
    public List<Customs> selectCustomsAll(String customsStatNo, String customsUnitMeas, String description) {
        return customsMapper.selectCustomsAll(customsStatNo, customsUnitMeas, description);
    }

    /**
     * 校验主键
     *
     * @param customs 海关统计
     * @return 结果
     */
    @Override
    public String checkCustomsCodeUnique(Customs customs) {
        Integer delflag = customs.getDelFlag()== null ? -1 : customs.getDelFlag();
        String rowkey = customs.getRowkey()== null ? "" : customs.getRowkey();
        Customs info = customsMapper.checkCustomsCodeUnique(customs.getCustomsStatNo());
        if(StringUtils.isNull(info)){
            return UserConstants.POST_NAME_UNIQUE;
        }else{
            if (rowkey.equals(info.getRowkey()))
            {
                return UserConstants.POST_NAME_UNIQUE;
            }
        }
        return UserConstants.POST_NAME_NOT_UNIQUE;
    }

    /**
     * 新增海关统计
     *
     * @param customs 海关统计
     * @return 结果
     */
    @Override
    public int insertCustomst(Customs customs) {
        if(customs.getDelFlag()==null||"".equals(customs.getDelFlag())){
            customs.setDelFlag(0);
        }
        return customsMapper.insertCustomst(customs);
    }
    /**
     * 修改海关统计
     *
     * @param customs 修改海关统计
     * @return 结果
     */
    @Override
    public int updateCustomst(Customs customs) {
        return customsMapper.updateCustomst(customs);
    }

    @Override
    public String importTransport(List<Customs> typeList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(typeList) || typeList.size() == 0)
        {
            throw new BusinessException("导入海关统计数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
//        String password = configService.selectConfigByKey("sys.user.initPassword");
        for (Customs customs : typeList)
        {
            try
            {
                // 验证是否存在这个运输单位类型
                Customs t = customsMapper.selectCustomsByCode(customs.getCustomsStatNo());
                if (StringUtils.isNull(t))
                {

                    customs.setCreateBy(operName);
                    this.insertCustomst(customs);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、海关统计 " + customs.getCustomsStatNo() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    customs.setUpdateBy(operName);
                    this.updateCustomst(customs);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、海关统计 " + customs.getCustomsStatNo() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、海关统计 " + customs.getCustomsStatNo() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、海关统计 " + customs.getCustomsStatNo() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<Customs> selectCustomstList(String ids) {
        return customsMapper.selectCustomstList(ids != null&&!"".equals(ids) ? Convert.toStrArray(ids) : null);
    }

    @Override
    public int deleteCustomsByIds(String ids) {
        if(ids!=null&&!"".equals(ids)){
            return customsMapper.deleteCustomsByIds(Convert.toStrArray(ids));
        }else{
            return -1;
        }
    }

    @Override
    public Customs selectCustomsByCode(String customsStatNo) {
        return customsMapper.selectCustomsByCode(customsStatNo);
    }
}
